<?php

class Ware extends CI_Controller {

    public function Ware()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('Waremodel');
        $this->load->model('Productmodel');
        $this->load->model('Usermodel');
        $this->load->library('session');
        $this->load->helper('form');
    }
	
    public function index()
    {
        if (isset($_GET['id']))
            $automat_id = $_GET['id'];
        else
            $automat_id = 1;
    	$username = $this->session->userdata('username');
        
	   
        if ($username) {
            $user_id = $this->Usermodel->getUserIdByLogin($username);
            $arr = array('automat_id' => $automat_id);
            $this->session->set_userdata($arr);
            $data = array ();
            $wares=$this->Waremodel->getAllWares();
            $products=$this->Productmodel->getAllProducts($user_id);
            
			$data['username']=$username;
	        $data['wares']=$wares;
            $data['products']=$products;
	        $data['automat_id']=$automat_id;
            $data['user_id']=$user_id;
            
            $this->load->view("ware_view",$data);
		}
		else 
			redirect('user/login');
	}
    
    public function add_ware()
    {
        $automat_id = $this->session->userdata('automat_id');
        $name;
        if (isset($_POST['name']))
        {
            $rules = array
            (
                array
                (   
                    'field' => 'name',
                    'label' => 'name',
                    'rules' => 'trim|required|xss_clean'
                )
            );
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules($rules);
            $val_res = $this->form_validation->run();
            
            
            $name = $_POST['name'];
            $pr_code = $this->get_product_code($name);
            if ($pr_code != -1)
            {
                if ($_POST['can_sell'] == 1) $can_sell = true;
                elseif ($_POST['can_sell'] == 0) $can_sell = false; 
                $this->Waremodel->addWare($_POST['price'],$_POST['amount'],$automat_id, $pr_code,$can_sell,$_POST['sell_from'], $_POST['sell_to']);
            }
               
        }
        redirect('ware?id='.$automat_id);
    }
    
    
    
    public function delete_ware()
    {
        $name = $_POST['name'];
        $automat_id =  $this->session->userdata('automat_id');
        
        $ware_id = $this->get_ware_id($name);
        if ($ware_id != -1)
            $this->Waremodel->deleteWare($ware_id);
        
        redirect('ware?id='.$automat_id);
        
    }
    
    
    public function get_ware_id($name)
    {
        $prod_code;
        $ware_id = -1;
        $username = $this->session->userdata('username');
        $user_id = $this->Usermodel->getUserIdByLogin($username);
        $products=$this->Productmodel->getAllProducts($user_id);
        $wares=$this->Waremodel->getAllWares();
        for ($i=0;$i<count($products);$i++){
            if ($products[$i]['Name'] == $name){
                $prod_code = $products[$i]['ProductCode'];
                break;
            }
        }
        if($i == count($products))
            return -1;
        for ($i=0;$i<count($wares);$i++){
            if ($wares[$i]['ProductCode'] == $prod_code){
                $ware_id =$wares[$i]['WareId'];
                return $ware_id;
            }
        }
        if($i == count($wares))
            return -1;
        
        return $ware_id;
        
    }
    
    public function get_product_code($name)
    {
        $prod_code = -1;
		$username = $this->session->userdata('username');
        $user_id = $this->Usermodel->getUserIdByLogin($username);
        $products=$this->Productmodel->getAllProducts($user_id);
        $wares=$this->Waremodel->getAllWares();
        for ($i=0;$i<count($products);$i++){
            if ($products[$i]['Name'] == $name){
                $prod_code = $products[$i]['ProductCode'];
                return $prod_code;
            }
        }
        return -1;
        
    }
    
    
    public function can_sell_ware()
    {
        $name;
        $can_sell;
        $automat_id = $this->session->userdata('automat_id');
        if (isset($_POST['name']))
        {
            $name = $_POST['name'];
            $ware_id = $this->get_ware_id($name);
            if (isset($_POST['can_sell']) && $ware_id != -1 && ($_POST['can_sell'] == 1 || $_POST['can_sell'] == 0))
            {
                if ($_POST['can_sell'] == 1) $can_sell = 1;
                else $can_sell = 0;
                $this->Waremodel->setCanSell($ware_id, $can_sell); 
            }
        }
        redirect('ware?id='.$automat_id);      
    }
    
    public function update_ware()
    {
        $name;
        if (isset($_POST['name']))
        {
            $name = $_POST['name'];
            $automat_id = $this->session->userdata('automat_id');
            $ware_id = $this->get_ware_id($name);
            $prod_code = $this->get_product_code($name);
            if ($ware_id != -1 && $prod_code != -1)
            {
                $price;
                $amount;
                $s_from;
                $s_to;
                
                if (isset($_POST['price'])) $price = $_POST['price'];
                else $price = null;        
                
                if (isset($_POST['amount'])) $amount = $_POST['amount'];
                else $amount = null;
                
                if (isset($_POST['s_from'])) $s_from = $_POST['s_from'];
                else $s_from = null;
                
                if (isset($_POST['s_to'])) $s_to = $_POST['s_to'];
                else $s_to = null;
                
                $this->Waremodel->updateWare($ware_id,$price,$amount,$automat_id,$prod_code,$s_from,$s_to);
                
            }
                
        }
        
        redirect("ware?id=".$automat_id);
    }
    
   
}

?>
